package Offer;

/**
 * 两个链表的第一个公共节点
 * @author 是阿秋啊
 * @date 2022/04/10 08:09
 **/
public class Num52 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode preA = headA;
        ListNode preB = headB;
        while (preA != null || preB != null) {
            if (preA == preB) {
                return preA;
            }
            if (preA == null) {
                preA = headB;
            } else {
                preA = preA.next;
            }
            if (preB == null) {
                preB = headA;
            } else {
                preB = preB.next;
            }
        }
        return null;
    }
}
